package com.example.sqlitetest;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

public class CookAdapter extends BaseAdapter {
    Context context;
    List<FoodBean> mDatas;

    public CookAdapter(Context context, List<FoodBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_lv,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        FoodBean bean = mDatas.get(position);
        holder.nameTv.setText(bean.getName());
        holder.infoTv.setText(bean.getInfo());
        holder.shicaiTv.setText(bean.getFood_str());
        holder.loveTv.setText(bean.getLove()+"");
        Picasso.with(context).load(bean.getPic()).into(holder.iv);
        return convertView;
    }

    class ViewHolder{
        ImageView iv;
        TextView nameTv,shicaiTv,infoTv,loveTv;
        public ViewHolder(View view){
            iv = view.findViewById(R.id.item_iv);
            nameTv = view.findViewById(R.id.item_tv_name);
            shicaiTv = view.findViewById(R.id.item_tv_shicai);
            infoTv = view.findViewById(R.id.item_tv_info);
            loveTv = view.findViewById(R.id.item_tv_love);
        }
    }
}